using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using RAG.Application.Interfaces;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using System.IO;
using RAG.Infrastructure.Data;
using RAG.Domain.Entities.App;
using Microsoft.EntityFrameworkCore;

namespace RAG.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SystemController : ControllerBase
{
    private readonly ILogger<SystemController> _logger;
    private readonly IDocumentProcessingService _documentProcessingService;
    private readonly RagDbContext _dbContext;
    private readonly IAppSystemService _systemService;

    public SystemController(ILogger<SystemController> logger, IDocumentProcessingService documentProcessingService, RagDbContext dbContext, IAppSystemService systemService)
    {
        _logger = logger;
        _documentProcessingService = documentProcessingService;
        _dbContext = dbContext;
        _systemService = systemService;
    }

    /// <summary>
    /// 系统健康检查
    /// </summary>
    [HttpGet("health")]
    [AllowAnonymous]
    public async Task<IActionResult> HealthCheck()
    {
        try
        {
            // 检查数据库连接
            var dbConnectionOk = await _dbContext.Database.CanConnectAsync();
            
            // 检查向量服务状态
            var vectorsCount = await _dbContext.Vectors.CountAsync();
            var vectorServiceOk = vectorsCount >= 0;
            
            // 检查文件存储状态
            var documentsCount = await _dbContext.Documents.CountAsync();
            var fileStorageOk = documentsCount >= 0;

            var healthInfo = new
            {
                // 前端期望的字段名和值
                systemHealth = dbConnectionOk ? "normal" : "abnormal",
                databaseStatus = dbConnectionOk ? "connected" : "disconnected",
                vectorServiceStatus = vectorServiceOk ? "available" : "unavailable",
                fileStorageStatus = fileStorageOk ? "normal" : "abnormal",
                
                // 原有字段
                Status = dbConnectionOk ? "Healthy" : "Unhealthy",
                Timestamp = DateTime.UtcNow,
                Version = "1.0.0",
                Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development",
                MachineName = Environment.MachineName,
                ProcessId = Environment.ProcessId,
                WorkingSet = GC.GetTotalMemory(false),
                SupportedFileTypes = _documentProcessingService.GetSupportedFileTypes()
            };

            _logger.LogInformation("系统健康检查通过");
            return Ok(new { code = 200, message = "系统运行正常", data = healthInfo });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "系统健康检查失败");
            return StatusCode(500, new { code = 500, message = "系统健康检查失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统信息
    /// </summary>
    [HttpGet("info")]
    [AllowAnonymous]
    public IActionResult GetSystemInfo()
    {
        try
        {
            var systemInfo = new
            {
                OS = Environment.OSVersion.ToString(),
                Framework = Environment.Version.ToString(),
                ProcessorCount = Environment.ProcessorCount,
                Is64BitProcess = Environment.Is64BitProcess,
                Is64BitOperatingSystem = Environment.Is64BitOperatingSystem,
                SystemPageSize = Environment.SystemPageSize,
                WorkingSet = Environment.WorkingSet,
                TotalPhysicalMemory = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes,
                ProcessStartTime = Process.GetCurrentProcess().StartTime,
                Uptime = DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime()
            };

            return Ok(new { code = 200, message = "获取系统信息成功", data = systemInfo });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统信息失败");
            return StatusCode(500, new { code = 500, message = "获取系统信息失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取支持的文件类型
    /// </summary>
    [HttpGet("supported-file-types")]
    [AllowAnonymous]
    public IActionResult GetSupportedFileTypes()
    {
        try
        {
            var supportedTypes = _documentProcessingService.GetSupportedFileTypes();
            return Ok(new { code = 200, message = "获取支持的文件类型成功", data = supportedTypes });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取支持的文件类型失败");
            return StatusCode(500, new { code = 500, message = "获取支持的文件类型失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 测试文档处理
    /// </summary>
    [HttpPost("test-document-processing")]
    [AllowAnonymous]
    public async Task<IActionResult> TestDocumentProcessing([FromBody] TestDocumentProcessingDto dto)
    {
        try
        {
            // 创建临时文件进行测试
            var tempFile = Path.GetTempFileName();
            await System.IO.File.WriteAllTextAsync(tempFile, dto.Content);

            try
            {
                // 测试文档处理
                var extractedText = await _documentProcessingService.ExtractTextAsync(tempFile, dto.FileType);
                var chunks = await _documentProcessingService.SplitIntoChunksAsync(extractedText, dto.ChunkSize);
                var summary = await _documentProcessingService.GenerateSummaryAsync(extractedText);

                var result = new
                {
                    OriginalContent = dto.Content,
                    ExtractedText = extractedText,
                    Chunks = chunks,
                    Summary = summary,
                    ChunkCount = chunks.Count,
                    IsValidFormat = _documentProcessingService.ValidateFileFormat($"test{dto.FileType}", dto.ContentType)
                };

                return Ok(new { code = 200, message = "文档处理测试成功", data = result });
            }
            finally
            {
                // 清理临时文件
                if (System.IO.File.Exists(tempFile))
                {
                    System.IO.File.Delete(tempFile);
                }
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "文档处理测试失败");
            return StatusCode(500, new { code = 500, message = "文档处理测试失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统统计信息
    /// </summary>
    [HttpGet("statistics")]
    [AllowAnonymous]
    public async Task<IActionResult> GetSystemStatistics()
    {
        try
        {
            var result = await _systemService.GetSystemStatisticsAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统统计信息失败");
            return StatusCode(500, new { code = 500, message = "获取系统统计信息失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统分析报告
    /// </summary>
    [HttpGet("analytics")]
    [AllowAnonymous]
    public async Task<IActionResult> GetSystemAnalytics()
    {
        try
        {
            var result = await _systemService.GetSystemAnalyticsAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统分析报告失败");
            return StatusCode(500, new { code = 500, message = "获取系统分析报告失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取向量化处理状态
    /// </summary>
    [HttpGet("vectorization-status")]
    [AllowAnonymous]
    public async Task<IActionResult> GetVectorizationStatus()
    {
        try
        {
            var result = await _systemService.GetVectorizationStatusAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取向量化处理状态失败");
            return StatusCode(500, new { code = 500, message = "获取向量化处理状态失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取仪表盘统计数据
    /// </summary>
    [HttpGet("dashboard-statistics")]
    [AllowAnonymous]
    public async Task<IActionResult> GetDashboardStatistics()
    {
        try
        {
            var result = await _systemService.GetDashboardStatisticsAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取仪表盘统计数据失败");
            return StatusCode(500, new { code = 500, message = "获取仪表盘统计数据失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统健康状态
    /// </summary>
    [HttpGet("health-status")]
    [AllowAnonymous]
    public async Task<IActionResult> GetSystemHealthStatus()
    {
        try
        {
            var result = await _systemService.GetSystemHealthStatusAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统健康状态失败");
            return StatusCode(500, new { code = 500, message = "获取系统健康状态失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统详细信息
    /// </summary>
    [HttpGet("detailed-info")]
    [AllowAnonymous]
    public async Task<IActionResult> GetSystemDetailedInfo()
    {
        try
        {
            var result = await _systemService.GetSystemDetailedInfoAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统详细信息失败");
            return StatusCode(500, new { code = 500, message = "获取系统详细信息失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取向量化详细状态
    /// </summary>
    [HttpGet("vectorization-detailed-status")]
    [AllowAnonymous]
    public async Task<IActionResult> GetVectorizationDetailedStatus()
    {
        try
        {
            var result = await _systemService.GetVectorizationDetailedStatusAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取向量化详细状态失败");
            return StatusCode(500, new { code = 500, message = "获取向量化详细状态失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 获取系统分析报告（扩展版）
    /// </summary>
    [HttpGet("analytics-extended")]
    [AllowAnonymous]
    public async Task<IActionResult> GetSystemAnalyticsExtended()
    {
        try
        {
            var result = await _systemService.GetSystemAnalyticsExtendedAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取系统分析报告失败");
            return StatusCode(500, new { code = 500, message = "获取系统分析报告失败", data = new { error = ex.Message } });
        }
    }

    /// <summary>
    /// 批量创建权限数据（仅管理员使用）
    /// </summary>
    [HttpPost("init-permissions")]
    [AllowAnonymous] // 临时允许匿名访问，后续需要改为管理员权限
    public async Task<IActionResult> InitializePermissions()
    {
        try
        {
            // 检查是否已经初始化过
            var existingPermissions = await _dbContext.Permissions.CountAsync();
            if (existingPermissions > 0)
            {
                return BadRequest(new { code = 400, message = "权限数据已存在，请勿重复初始化", data = new { existingCount = existingPermissions } });
            }

            var permissions = new List<Permission>
            {
                // 用户管理权限
                new Permission
                {
                    PermissionCode = "user.view.all",
                    PermissionName = "查看所有用户",
                    PermissionDescription = "超级管理员查看所有用户列表"
                },
                new Permission
                {
                    PermissionCode = "user.create",
                    PermissionName = "创建用户",
                    PermissionDescription = "超级管理员创建新用户"
                },
                new Permission
                {
                    PermissionCode = "user.update.all",
                    PermissionName = "更新所有用户",
                    PermissionDescription = "超级管理员更新任何用户信息"
                },
                new Permission
                {
                    PermissionCode = "user.delete",
                    PermissionName = "删除用户",
                    PermissionDescription = "超级管理员删除用户"
                },
                new Permission
                {
                    PermissionCode = "user.manage",
                    PermissionName = "用户管理",
                    PermissionDescription = "启用/禁用用户，分配角色"
                },
                new Permission
                {
                    PermissionCode = "user.view.self",
                    PermissionName = "查看自己",
                    PermissionDescription = "用户查看自己的信息"
                },
                new Permission
                {
                    PermissionCode = "user.update.self",
                    PermissionName = "更新自己",
                    PermissionDescription = "用户更新自己的信息"
                },

                // 角色管理权限（仅超级管理员）
                new Permission
                {
                    PermissionCode = "role.view",
                    PermissionName = "角色查看",
                    PermissionDescription = "查看角色列表和详情"
                },
                new Permission
                {
                    PermissionCode = "role.create",
                    PermissionName = "角色创建",
                    PermissionDescription = "创建新角色"
                },
                new Permission
                {
                    PermissionCode = "role.update",
                    PermissionName = "角色更新",
                    PermissionDescription = "更新角色信息"
                },
                new Permission
                {
                    PermissionCode = "role.delete",
                    PermissionName = "角色删除",
                    PermissionDescription = "删除角色"
                },
                new Permission
                {
                    PermissionCode = "role.manage",
                    PermissionName = "角色管理",
                    PermissionDescription = "分配权限给角色"
                },

                // 权限管理权限（仅超级管理员）
                new Permission
                {
                    PermissionCode = "permission.view",
                    PermissionName = "权限查看",
                    PermissionDescription = "查看权限列表和详情"
                },
                new Permission
                {
                    PermissionCode = "permission.create",
                    PermissionName = "权限创建",
                    PermissionDescription = "创建新权限"
                },
                new Permission
                {
                    PermissionCode = "permission.update",
                    PermissionName = "权限更新",
                    PermissionDescription = "更新权限信息"
                },
                new Permission
                {
                    PermissionCode = "permission.delete",
                    PermissionName = "权限删除",
                    PermissionDescription = "删除权限"
                },
                new Permission
                {
                    PermissionCode = "permission.manage",
                    PermissionName = "权限管理",
                    PermissionDescription = "启用/禁用权限"
                },

                // 文档管理权限
                new Permission
                {
                    PermissionCode = "document.create",
                    PermissionName = "文档创建",
                    PermissionDescription = "上传和创建文档"
                },
                new Permission
                {
                    PermissionCode = "document.view.own",
                    PermissionName = "查看自己的文档",
                    PermissionDescription = "查看自己上传的文档"
                },
                new Permission
                {
                    PermissionCode = "document.view.all",
                    PermissionName = "查看所有文档",
                    PermissionDescription = "查看所有用户的文档"
                },
                new Permission
                {
                    PermissionCode = "document.update.own",
                    PermissionName = "更新自己的文档",
                    PermissionDescription = "更新自己上传的文档"
                },
                new Permission
                {
                    PermissionCode = "document.update.all",
                    PermissionName = "更新所有文档",
                    PermissionDescription = "更新任何用户的文档"
                },
                new Permission
                {
                    PermissionCode = "document.delete.own",
                    PermissionName = "删除自己的文档",
                    PermissionDescription = "删除自己上传的文档"
                },
                new Permission
                {
                    PermissionCode = "document.delete.all",
                    PermissionName = "删除所有文档",
                    PermissionDescription = "删除任何用户的文档"
                },
                new Permission
                {
                    PermissionCode = "document.upload.batch",
                    PermissionName = "批量上传文档",
                    PermissionDescription = "批量上传多个文档"
                },
                new Permission
                {
                    PermissionCode = "document.manage",
                    PermissionName = "文档管理",
                    PermissionDescription = "管理文档权限和状态"
                },

                // 知识库管理权限
                new Permission
                {
                    PermissionCode = "knowledgebase.create",
                    PermissionName = "知识库创建",
                    PermissionDescription = "创建新的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.view.own",
                    PermissionName = "查看自己的知识库",
                    PermissionDescription = "查看自己创建的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.view.all",
                    PermissionName = "查看所有知识库",
                    PermissionDescription = "查看所有用户的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.update.own",
                    PermissionName = "更新自己的知识库",
                    PermissionDescription = "更新自己创建的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.update.all",
                    PermissionName = "更新所有知识库",
                    PermissionDescription = "更新任何用户的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.delete.own",
                    PermissionName = "删除自己的知识库",
                    PermissionDescription = "删除自己创建的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.delete.all",
                    PermissionName = "删除所有知识库",
                    PermissionDescription = "删除任何用户的知识库"
                },
                new Permission
                {
                    PermissionCode = "knowledgebase.manage",
                    PermissionName = "知识库管理",
                    PermissionDescription = "管理知识库中的文档"
                },

                // RAG和问答权限
                new Permission
                {
                    PermissionCode = "qa.create",
                    PermissionName = "创建问答",
                    PermissionDescription = "创建新的问答记录"
                },
                new Permission
                {
                    PermissionCode = "qa.generate",
                    PermissionName = "生成答案",
                    PermissionDescription = "使用RAG系统生成答案"
                },
                new Permission
                {
                    PermissionCode = "qa.retrieve",
                    PermissionName = "检索文档",
                    PermissionDescription = "检索相关文档片段"
                },
                new Permission
                {
                    PermissionCode = "qa.view.own",
                    PermissionName = "查看自己的问答",
                    PermissionDescription = "查看自己的问答记录"
                },
                new Permission
                {
                    PermissionCode = "qa.view.all",
                    PermissionName = "查看所有问答",
                    PermissionDescription = "查看所有用户的问答记录"
                },
                new Permission
                {
                    PermissionCode = "qa.delete.own",
                    PermissionName = "删除自己的问答",
                    PermissionDescription = "删除自己的问答记录"
                },
                new Permission
                {
                    PermissionCode = "qa.delete.all",
                    PermissionName = "删除所有问答",
                    PermissionDescription = "删除任何用户的问答记录"
                },
                new Permission
                {
                    PermissionCode = "qa.conversation.view.own",
                    PermissionName = "查看自己的对话",
                    PermissionDescription = "查看自己的对话历史"
                },
                new Permission
                {
                    PermissionCode = "qa.conversation.view.all",
                    PermissionName = "查看所有对话",
                    PermissionDescription = "查看所有用户的对话历史"
                },
                new Permission
                {
                    PermissionCode = "qa.session.view.own",
                    PermissionName = "查看自己的会话",
                    PermissionDescription = "查看自己的会话列表"
                },
                new Permission
                {
                    PermissionCode = "qa.session.view.all",
                    PermissionName = "查看所有会话",
                    PermissionDescription = "查看所有用户的会话列表"
                },
                new Permission
                {
                    PermissionCode = "qa.session.create",
                    PermissionName = "创建会话",
                    PermissionDescription = "创建新的问答会话"
                },
                new Permission
                {
                    PermissionCode = "qa.session.message",
                    PermissionName = "发送消息",
                    PermissionDescription = "在会话中发送消息"
                },
                new Permission
                {
                    PermissionCode = "qa.session.manage",
                    PermissionName = "会话管理",
                    PermissionDescription = "管理RAG会话"
                },
                new Permission
                {
                    PermissionCode = "qa.manage",
                    PermissionName = "问答管理",
                    PermissionDescription = "管理问答系统"
                },

                // 向量管理权限
                new Permission
                {
                    PermissionCode = "vector.view.own",
                    PermissionName = "查看自己的向量",
                    PermissionDescription = "查看自己创建的向量"
                },
                new Permission
                {
                    PermissionCode = "vector.view.all",
                    PermissionName = "查看所有向量",
                    PermissionDescription = "查看所有用户的向量"
                },
                new Permission
                {
                    PermissionCode = "vector.create",
                    PermissionName = "创建向量",
                    PermissionDescription = "创建新的向量"
                },
                new Permission
                {
                    PermissionCode = "vector.delete.own",
                    PermissionName = "删除自己的向量",
                    PermissionDescription = "删除自己创建的向量"
                },
                new Permission
                {
                    PermissionCode = "vector.delete.all",
                    PermissionName = "删除所有向量",
                    PermissionDescription = "删除任何用户的向量"
                },
                new Permission
                {
                    PermissionCode = "vector.generate",
                    PermissionName = "生成向量",
                    PermissionDescription = "生成单个向量"
                },
                new Permission
                {
                    PermissionCode = "vector.generate.batch",
                    PermissionName = "批量生成向量",
                    PermissionDescription = "批量生成多个向量"
                },
                new Permission
                {
                    PermissionCode = "vector.similarity",
                    PermissionName = "计算相似度",
                    PermissionDescription = "计算向量相似度"
                },
                new Permission
                {
                    PermissionCode = "vector.search",
                    PermissionName = "向量搜索",
                    PermissionDescription = "搜索相似向量"
                },

                // 系统管理权限
                new Permission
                {
                    PermissionCode = "system.view",
                    PermissionName = "系统查看",
                    PermissionDescription = "查看系统信息和统计"
                },
                new Permission
                {
                    PermissionCode = "system.manage",
                    PermissionName = "系统管理",
                    PermissionDescription = "管理系统设置和配置"
                }
            };

            await _dbContext.Permissions.AddRangeAsync(permissions);
            await _dbContext.SaveChangesAsync();

            _logger.LogInformation("权限数据初始化成功，共创建 {Count} 个权限", permissions.Count);

            return Ok(new { code = 200, message = "权限数据初始化成功", data = new { count = permissions.Count } });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "权限数据初始化失败");
            return StatusCode(500, new { code = 500, message = "权限数据初始化失败", data = new { error = ex.Message } });
        }
        }

    /// <summary>
    /// 初始化角色和权限分配
    /// </summary>
    [HttpPost("initialize-roles-permissions")]
    [AllowAnonymous] // 临时允许匿名访问，后续需要改为管理员权限
    public async Task<IActionResult> InitializeRolesAndPermissions()
    {
        try
        {
            // 检查是否已经初始化过
            if (await _dbContext.Roles.AnyAsync())
            {
                return Ok(new { code = 200, message = "角色数据已存在，无需重复初始化" });
            }

            // 获取所有权限
            var allPermissions = await _dbContext.Permissions.ToListAsync();
            if (!allPermissions.Any())
            {
                return BadRequest(new { code = 400, message = "请先初始化权限数据" });
            }

            // 创建超级管理员角色（拥有所有权限）
            var superAdminRole = new Role
            {
                RoleName = "超级管理员",
                RoleDescription = "系统超级管理员，拥有所有权限",
                Permissions = allPermissions // 超级管理员拥有所有60个权限
            };

            // 创建管理员角色
            var adminPermissions = allPermissions.Where(p => 
                p.PermissionCode.StartsWith("document.") ||
                p.PermissionCode.StartsWith("knowledgebase.") ||
                p.PermissionCode.StartsWith("qa.") ||
                p.PermissionCode.StartsWith("vector.") ||
                p.PermissionCode.StartsWith("system.view") ||
                p.PermissionCode.StartsWith("user.view") ||
                p.PermissionCode.StartsWith("user.update.self")
            ).ToList();

            var adminRole = new Role
            {
                RoleName = "管理员",
                RoleDescription = "系统管理员，拥有文档、知识库、问答管理权限",
                Permissions = adminPermissions
            };

            // 创建普通用户角色
            var userPermissions = allPermissions.Where(p => 
                p.PermissionCode.StartsWith("document.view.own") ||
                p.PermissionCode.StartsWith("document.create") ||
                p.PermissionCode.StartsWith("document.update.own") ||
                p.PermissionCode.StartsWith("document.delete.own") ||
                p.PermissionCode.StartsWith("qa.") ||
                p.PermissionCode.StartsWith("user.view.self") ||
                p.PermissionCode.StartsWith("user.update.self")
            ).ToList();

            var userRole = new Role
            {
                RoleName = "普通用户",
                RoleDescription = "普通用户，拥有基本的文档和问答权限",
                Permissions = userPermissions
            };

            // 添加角色到数据库
            await _dbContext.Roles.AddRangeAsync(superAdminRole, adminRole, userRole);
            await _dbContext.SaveChangesAsync();

            _logger.LogInformation("角色和权限分配初始化成功，共创建 3 个角色");

            return Ok(new { code = 200, message = "角色和权限分配初始化成功", data = new { rolesCount = 3 } });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "角色和权限分配初始化失败");
            return StatusCode(500, new { code = 500, message = "角色和权限分配初始化失败", data = new { error = ex.Message } });
        }
    }
}

/// <summary>
/// 测试文档处理DTO
/// </summary>
public class TestDocumentProcessingDto
{
    public string Content { get; set; } = "";
    public string FileType { get; set; } = ".txt";
    public string ContentType { get; set; } = "text/plain";
    public int ChunkSize { get; set; } = 1000;
}
